package algo4th.unionFind;

/**
 * quick union implementation
 */
public class QuickUnionUF extends UF {

    /**
     * The constructor of UF
     *
     * @param N the size of the pool
     */
    public QuickUnionUF(int N) {
        super(N);
    }

    @Override
    public int find(int p) {
        while (p != id[p]) p = id[p];
        return p;
    }

    @Override
    public void union(int p, int q) {
        int pRoot = find(p);
        int qRoot = find(q);
        if (pRoot == qRoot) return;

        id[pRoot] = qRoot;

        count--;
    }
}
